package com.nopay.dxdwphone.net;

import android.util.Log;
import java.util.concurrent.TimeUnit;

import com.nopay.dxdwphone.Constants;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @ProjectName: guizhou_assets
 * @Package: dhc.guizhou.assets.http
 * @ClassName: AssetsRetrofit
 * @Description: 网络模块
 * @Author: Kawaii's cat
 * @CreateDate: 2020-5-25 9:36
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020-5-25 9:36
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class AssetsRetrofit {

    private static volatile AssetsRetrofit myRetrofit;

    public static AssetsRetrofit getInstance() {
        // 对象实例化时与否判断（不使用同步代码块，instance不等于null时，直接返回对象，提高运行效率）
        if (myRetrofit == null) {
            //同步代码块（对象未初始化时，使用同步代码块，保证多线程访问时对象在第一次创建后，不再重复被创建）
            synchronized (AssetsRetrofit.class) {
                //未初始化，则初始instance变量
                if (myRetrofit == null) {
                    myRetrofit = new AssetsRetrofit();
                }
            }
        }
        return myRetrofit;
    }


    public Retrofit getRetrofit() {

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Log.d("OkHttp", message);
            }
        });

        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new LoginStatusInterceptor())
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit;
    }

}

